package com.uls.ycl.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uls.ycl.mapper.RepairMaterialMapper;
import com.uls.ycl.domain.RepairMaterial;
import com.uls.ycl.service.IRepairMaterialService;

/**
 * 材料Service业务层处理
 * 
 * @author ycl
 * @date 2024-11-09
 */
@Service
public class RepairMaterialServiceImpl implements IRepairMaterialService 
{
    @Autowired
    private RepairMaterialMapper repairMaterialMapper;

    /**
     * 查询材料
     * 
     * @param materialId 材料主键
     * @return 材料
     */
    @Override
    public RepairMaterial selectRepairMaterialByMaterialId(Long materialId)
    {
        return repairMaterialMapper.selectRepairMaterialByMaterialId(materialId);
    }

    /**
     * 查询材料列表
     * 
     * @param repairMaterial 材料
     * @return 材料
     */
    @Override
    public List<RepairMaterial> selectRepairMaterialList(RepairMaterial repairMaterial)
    {
        return repairMaterialMapper.selectRepairMaterialList(repairMaterial);
    }

    /**
     * 新增材料
     * 
     * @param repairMaterial 材料
     * @return 结果
     */
    @Override
    public int insertRepairMaterial(RepairMaterial repairMaterial)
    {
        return repairMaterialMapper.insertRepairMaterial(repairMaterial);
    }

    /**
     * 修改材料
     *
     * @param repairMaterial 材料
     * @return 结果
     */
    @Override
    public int updateRepairMaterial(RepairMaterial repairMaterial)
    {
        repairMaterial.setUpdateAt(new Date());
        BigDecimal total = Optional.ofNullable(repairMaterial.getTotal())
                .map(BigDecimal::new)
                .orElse(BigDecimal.ZERO);
        BigDecimal price = Optional.ofNullable(repairMaterial.getPrice())
                .orElse(BigDecimal.ZERO);
        // 检查材料数量是否为0
        if (total.compareTo(BigDecimal.ZERO) == 0) {
            repairMaterial.setTotalPrice(BigDecimal.ZERO);
        } else {
            repairMaterial.setTotalPrice(total.multiply(price));
        }
        // 更新材料信息
        return repairMaterialMapper.updateRepairMaterial(repairMaterial);
    }


    /**
     * 批量删除材料
     * 
     * @param materialIds 需要删除的材料主键
     * @return 结果
     */
    @Override
    public int deleteRepairMaterialByMaterialIds(Long[] materialIds)
    {
        return repairMaterialMapper.deleteRepairMaterialByMaterialIds(materialIds);
    }

    /**
     * 删除材料信息
     * 
     * @param materialId 材料主键
     * @return 结果
     */
    @Override
    public int deleteRepairMaterialByMaterialId(Long materialId)
    {
        return repairMaterialMapper.deleteRepairMaterialByMaterialId(materialId);
    }
}
